package com.zhima.controller.plain;

import com.zhima.annotation.MustLogin;
import com.zhima.dto.QueryJudgeRecordDTO;
import com.zhima.dto.SubmitJudgeDTO;
import com.zhima.dto.ZhimaOjPageResult;
import com.zhima.dto.ZhimaOjResult;
import com.zhima.service.OjJudgeRecordService;
import com.zhima.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhima
 * @since 2023-09-05
 */
@RestController
@RequestMapping("/judge-record")
public class OjJudgeRecordController {
    @Autowired
    private OjJudgeRecordService ojJudgeRecordService;

    /**
     * 得到指定评测记录
     * 出题人可以看到所有的提交记录
     * 做题人可以看到自己的提交记录
     * 组织竞赛的人可以看到本次比赛中的题目的提交（这个提交出题人和做题人都可见）
     *
     * @param judgeRecordId
     * @return
     */
    @GetMapping("/r/{judgeRecordId}")
    @MustLogin
    public ZhimaOjResult queryJudgeRecordById(@PathVariable("judgeRecordId") Long judgeRecordId) {
        return ZhimaOjResult.success(ojJudgeRecordService.queryJudgeRecordById(UserHolder.get().getId(), judgeRecordId));
    }

    /**
     * 创建测评记录，即提交测评请求
     *
     * @param submitJudgeDTO
     * @return
     */
    @PostMapping
    public ZhimaOjResult createJudgeRecord(@RequestBody SubmitJudgeDTO submitJudgeDTO) {
        return ZhimaOjResult.success(ojJudgeRecordService.createJudgeRecord(UserHolder.get(), submitJudgeDTO));
    }

    /**
     * 多条件搜索提交记录
     * 分页查询提交记录都能查，但是查看详情必须遵守一定可见性规则
     *
     * @param queryJudgeRecordDTO
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/c/{pageNum}/{pageSize}")
    public ZhimaOjPageResult pageQueryJudgeRecordBySomeConditions(@RequestBody QueryJudgeRecordDTO queryJudgeRecordDTO, @PathVariable("pageNum") Long pageNum, @PathVariable("pageSize") Long pageSize) {
        return ZhimaOjPageResult.success(ojJudgeRecordService.pageQueryJudgeRecordBySomeConditions(queryJudgeRecordDTO, pageNum, pageSize));
    }
}
